layui.use([ 'element', 'form','laydate' ], function() {
	var element = layui.element(), form = layui.form(),laydate = layui.laydate;
	
	form.on('submit(formSubmit)', function(data) {
		console.info(data.field);
		var dataParam = data.field;
		
		var assetTypeSelect = '';
		$("select[name='assetTypeSelect']").each(function(){
			assetTypeSelect = assetTypeSelect + $(this).val()+",";
		});
		if(assetTypeSelect.length > 0){
			assetTypeSelect = assetTypeSelect.substring(0, assetTypeSelect.length-1);
		}
		
		var assetModelSelect = '';
		$("select[name='assetModelSelect']").each(function(){
			assetModelSelect = assetModelSelect + $(this).val()+",";
		});
		if(assetModelSelect.length > 0){
			assetModelSelect = assetModelSelect.substring(0, assetModelSelect.length-1);
		}
		
		var assetTypeNum = ''
		$("input[name='assetTypeNum']").each(function(){
			assetTypeNum = assetTypeNum + $(this).val()+",";
		});
		if(assetTypeNum.length > 0){
			assetTypeNum = assetTypeNum.substring(0, assetTypeNum.length-1);
		}
		
		dataParam.assetTypeSelect = assetTypeSelect;
		dataParam.assetModelSelect = assetModelSelect;
		dataParam.assetTypeNum = assetTypeNum;
		console.info(dataParam);
		$.post(rootPath + "/assetin/assetInAdd.shtml", dataParam, function(result) {
			if (!result.status) {
				layer.msg(result.message, {
					icon : 5,
					shift : 6
				});
			} else {
				var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
				parent.layer.close(index); //再执行关闭   
				parent.layer.msg('添加成功', {
					icon : 1,time: 5000,shade: [0.8, '#393D49'],shadeClose:true
				});
				parent.refreshAsstInList();

			}
		}, "json");
		return false;
	});
	
	
	//对select1绑定事件
	assetTypeSelectBindChange('select(assetTypeSel_1)');

    form.on('select(assetModelCheck)', function(data){
    	console.log(data);
        var currAssetModel = data.value;
        var isHav = 0;
        $("select[name='assetModelSelect']").each(function(){
        	if(currAssetModel == this.value){
                isHav++;
			}
		});
        if(isHav > 1){
            layer.msg("您已选择该资产型号!", {icon: 5});
			$("#"+data.elem.id).val("");
            form.render();//重新渲染界面上的select
		}
    });

});


function getSelectOption (value,text){
	var option = $("<option>");
	option.attr("value",value);
	option.append(text);
	return option;
}

//对所有的select绑定事件
function assetTypeSelectBindChange(selectId){
	var form = layui.form();
	console.info(selectId);
	//对所有的select绑定事件
	form.on(selectId, function(data){
		console.info(data);
		var assetTypeSelectId = data.elem.id;
		var assetTypeVal = data.value;
		
		var assetTypeSelectIdArr = assetTypeSelectId.split('_');
		var assetModelId = "assetModelSel_"+assetTypeSelectIdArr[1];
		console.info(assetModelId);
		//根据资产类型id，查询当前类型下的资产型号
		 $.ajax({
             type: 'POST',
             url: rootPath + '/assetmodel/getAssetModelByType.shtml',
             data: {assetTypeID:assetTypeVal},
             dataType:  'json',
             success: function(data){
                
               $("#"+assetModelId).find("option").remove();
               $("#"+assetModelId).append(getSelectOption('','型号选择'));
               if(null != data && data.length > 0){
            	 
            	   $.each(data, function(key, val) {
            		   $("#"+assetModelId).append(getSelectOption(val.id,val.assetModelName));
               	      /*var option1 = $("<option>").val(val.areaId).text(val.fullname);
                           $("#City").append(option1);*/
                           form.render('select');
                   }); 
                   //$("#City").get(0).selectedIndex=0;
               }
               form.render('select');
            }
	    }); 
	});
	form.render('select');
}


function assetDtnFun(btnObj){
	var btnId = $(btnObj).attr("id");
	var btnArr = btnId.split('_');
	$("#item_tr_"+btnArr[1]).remove();
};

$(function(){
	
	//资产详情事件
	$('#addAssetType').click(function() {

		if($('tr[id*="item_tr_"]').length>=10){
			alert("详情数量不能多余十个！");
			return;
		}
		
		var assetTypeAllJson  = JSON.parse(assetTypeAlls.replace(/'/g,'"'));
		
		var trTotal = $("tr[id*='item_tr_']");
		$("#itemTable").append(assetType.initAeestType((trTotal.length+1),assetTypeAllJson));

		assetTypeSelectBindChange("select(assetTypeSel_"+(trTotal.length+1)+")");
		var form = layui.form();//只有执行了这一步，部分表单元素才会自动修饰成功
        form.render();//重新渲染界面上的select
	});
	
	var assetType = {
			
			initAeestType:function(curTrNum,assetTypeAllJson){
				var div = $("<div>");
				var tr=$("<tr>");
				
				var td1=$("<td>");
				var div1=$("<div class='layui-form-item'>");
				var lable1="<label class='layui-form-label'><font color='red'>*</font>类型</label>";
				var div2=$("<div class='layui-input-inline'>");//class='layui-input-inline'
				var select1=$("<select id='assetTypeSel_"+curTrNum+"' lay-filter='assetTypeSel_"+curTrNum+"' name='assetTypeSelect' required lay-verify='required'>");
				
				
				var tdModel=$("<td>");
				var divModel1=$("<div class='layui-form-item'>");
				var lableModel="<label class='layui-form-label'><font color='red'>*</font>型号</label>";
				var divModel2=$("<div class='layui-input-inline'>");//class='layui-input-inline'
				var selectModel=$("<select id='assetModelSel_"+curTrNum+"' lay-filter='assetModelCheck'  name='assetModelSelect' required lay-verify='required'>");
				
				
				var td2=$("<td>");
				var div3=$("<div class='layui-form-item'>");
				var lable2="<label class='layui-form-label'><font color='red'>*</font>数量</label>";
				var div4=$("<div class='layui-input-inline'>");
				var numInput=$("<input type='number' name='assetTypeNum' lay-verify='required|number'  required placeholder='输入个数量吧' autocomplete='off' class='layui-input'>");
				
				var td3=$("<td>");
				//var delBtn = $("<input type='button' value='删除' id='addAssetType' class=''></input>");
				//var delBtn = $("<button class='layui-btn layui-btn-sm layui-btn-normal'>删除</button>");
				//var delBtn = $("<button  class='layui-btn layui-btn-primary layui-btn-sm'>删除</button>");
				/*var btnDiv1=$("<div class='layui-form-item'>");
				var btnDiv2=$("<div class='layui-input-inline'>");*/
				var delBtn = $("<input type='button' onclick='assetDtnFun(this)'  style='height:30px;line-height:30px;text-align:center;' value='删除'  class='layui-btn layui-btn-primary layui-btn-xs'></input>");
				
				
				//设置trID
				tr.attr("id","item_tr_"+curTrNum);
				
				//拼接原生select
				var selectObj = this.getSelectObj(select1,assetTypeAllJson);
				//将原生select添加到div中
				div2.append(selectObj);
				
				div1.append(lable1);
				div1.append(div2);
				td1.append(div1);
				
				selectModel.append(this.getSelectOption("","型号选择"));
				divModel2.append(selectModel);
				divModel1.append(lableModel);
				divModel1.append(divModel2);
				tdModel.append(divModel1);
				
				
				div4.append(numInput);
				div3.append(lable2);
				div3.append(div4);
				td2.append(div3);
				
				delBtn.attr('id','dtn_'+curTrNum);
				/*btnDiv2.append(delBtn);
				btnDiv1.append(btnDiv2);*/
				td3.append(delBtn);
				
				tr.append(td1);
				tr.append(tdModel);
				tr.append(td2);
				tr.append(td3);
				div.append(tr)
				
				return div.html();
				/*this.div.attr("id","item_tr_div_"+curTrNum);
				this.div.append(this.tr);
				return this.div;*/
			},
			getSelectObj:function(select,assetTypeAllJson){
				select.append(this.getSelectOption('','搜索选择'));
				for(var i=0;i<assetTypeAllJson.length;i++){
					var asseType = assetTypeAllJson[i];
					select.append(this.getSelectOption(asseType.id,asseType.name));
				}
				return select;
			},
			
			getSelectOption:function(value,text){
				var option = $("<option>");
				option.attr("value",value);
				option.append(text);
				return option;
			}
	};

});
